package com.luych.toolbox.base.service.controller;

import com.luych.toolbox.base.service.entity.User;
import com.luych.toolbox.base.service.service.UserService;
import com.luych.toolbox.common.feign.intf.base.service.UserFeign;
import com.luych.toolbox.common.feign.param.base.service.UserParam;
import com.luych.toolbox.common.feign.view.base.service.UserView;
import com.luych.toolbox.common.page.CommonPageParam;
import com.luych.toolbox.common.page.CommonPageResult;
import com.luych.toolbox.common.page.CommonSort;
import com.luych.toolbox.common.page.PageResult;
import com.luych.toolbox.common.service.base.BaseController;
import com.luych.toolbox.common.service.exception.HttpException;
import com.luych.toolbox.common.service.security.SecurityUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(tags = "用户")
@RestController
@RequestMapping("/user")
public class UserController
        extends BaseController<UserView, UserParam, User>
        implements UserFeign {

    private final UserService userService;

    @Autowired
    public UserController(UserService userService) {
        super(userService);
        this.userService = userService;
    }

    @Override
    public void remove(String id) {
        UserView userView = userService.get(id)
                .orElseThrow(() -> new HttpException(HttpStatus.NOT_FOUND, "Can't find UserView with id: " + id));
        userService.removeCacheById(userView.getId());
        userService.removeCacheByLoginName(userView.getLoginName());
        super.remove(id);
    }

    @Override
    public void addRole(String userId, String roleId) {
        userService.addRole(userId, roleId);
    }

    @Override
    public void removeRole(String userId, String roleId) {
        userService.removeRole(userId, roleId);
    }

    @Override
    public UserView getByLoginName(String loginName) {
        return userService.getByLoginName(loginName).orElse(null);
    }

    @Override
    public CommonPageResult<UserView> pageByKeyword(long pageSize, long pageNumber, String sort, String order, String keyword) {
        CommonPageParam commonPageParam = CommonPageParam.getInstance().setPageSize(pageSize).setPageNumber(pageNumber);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            commonPageParam.addSort(CommonSort.getInstance().setSort(sort).setOrder(order));
        }
        PageResult<UserView> pageResult = userService.pageByKeyword(commonPageParam, keyword);
        return CommonPageResult.getInstance(pageResult);
    }

    @Override
    public List<UserView> online() {
        return userService.online();
    }

    @ApiOperation("查询当前登录信息")
    @GetMapping("/session")
    public UserView session() {
        UserView session = SecurityUtil.getSession();
        if (session == null) {
            throw new HttpException(HttpStatus.UNAUTHORIZED);
        }
        return session;
    }

    @ApiOperation("心跳")
    @GetMapping("/heartbeat")
    public void heartbeat() {

    }
}
